package com.example.demo.nuoqi.controller;

import com.example.demo.nuoqi.pojo.*;
import com.example.demo.nuoqi.service.FavoritesService;
import com.example.demo.nuoqi.service.ItemdetailsService;
import com.example.demo.nuoqi.service.ProblemmanagementService;
import com.example.demo.nuoqi.service.ResumemanagementService;
import com.example.demo.nuoqi.utils.Result;
import com.example.demo.nuoqi.utils.StatusCode;
import com.example.demo.nuoqi.utils.UserUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "FavoritesController")
@RestController
@RequestMapping("/favorites")
@CrossOrigin
public class FavoritesController {

    @Autowired
    private FavoritesService favoritesService;
    @Autowired
    private ItemdetailsService itemdetailsService;
    @Autowired
    private ProblemmanagementService problemmanagementService;
    @Autowired
    private ResumemanagementService resumemanagementService;

    /***
     * Favorites分页条件搜索实现
     * @param favorites
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Favorites条件分页查询",notes = "分页条件查询Favorites方法详情",tags = {"FavoritesController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Favorites对象",value = "传入JSON数据",required = false) Favorites favorites, @PathVariable int page, @PathVariable int size){
        //调用FavoritesService实现分页条件查询Favorites
        PageInfo<Favorites> pageInfo = favoritesService.findPage(favorites, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Favorites分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Favorites分页查询",notes = "分页查询Favorites方法详情",tags = {"FavoritesController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size){
        //调用FavoritesService实现分页查询Favorites
        PageInfo<Favorites> pageInfo = favoritesService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     *
     * @param favorites
     * @return
     */
    @ApiOperation(value = "Favorites条件查询",notes = "条件查询Favorites方法详情",tags = {"FavoritesController"})
    @PostMapping(value = "/search" )
    public Result<List<Favorites>> findList(@RequestBody(required = false) @ApiParam(name = "Favorites对象",value = "传入JSON数据",required = false) Favorites favorites){
        //调用FavoritesService实现条件查询Favorites
        List<Favorites> list = favoritesService.findList(favorites);
        return new Result<List<Favorites>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Favorites根据ID删除",notes = "根据ID删除Favorites方法详情",tags = {"FavoritesController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用FavoritesService实现根据主键删除
        favoritesService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Favorites数据
     * @param favorites
     * @param id
     * @return
     */
    @ApiOperation(value = "Favorites根据ID修改",notes = "根据ID修改Favorites方法详情",tags = {"FavoritesController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Favorites对象",value = "传入JSON数据",required = false) Favorites favorites, @PathVariable Integer id){
        //设置主键值
        favorites.setId(id);
        //调用FavoritesService实现修改Favorites
        favoritesService.update(favorites);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Favorites数据
     * @param favorites
     * @return
     */
    @ApiOperation(value = "Favorites添加",notes = "添加Favorites方法详情",tags = {"FavoritesController"})
    @PostMapping
    public Result add(@RequestBody @ApiParam(name = "Favorites对象",value = "传入JSON数据",required = true) Favorites favorites, HttpSession session){
        //调用FavoritesService实现添加Favorites
        User user = UserUtils.getUser(session);
        favorites.setUserId(user.getId());
        favoritesService.add(favorites);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Favorites数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Favorites根据ID查询",notes = "根据ID查询Favorites方法详情",tags = {"FavoritesController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<Favorites> findById(@PathVariable Integer id){
        //调用FavoritesService实现根据主键查询Favorites
        Favorites favorites = favoritesService.findById(id);
        return new Result<Favorites>(true,StatusCode.OK,"查询成功",favorites);
    }

    /***
     * 查询Favorites全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Favorites",notes = "查询所Favorites有方法详情",tags = {"FavoritesController"})
    @GetMapping
    public Result<List<Favorites>> findAll(){
        //调用FavoritesService实现查询所有Favorites
        List<Favorites> list = favoritesService.findAll();
        return new Result<List<Favorites>>(true, StatusCode.OK,"查询成功",list) ;
    }


    /***
     * 获取当前用户的收藏简历，项目，问题
     * @return
     */
    @ApiOperation(value = "查询所有Favorites",notes = "查询所Favorites有方法详情",tags = {"FavoritesController"})
    @GetMapping("findByUserFavorites")
    public Result<List<Favorites>> findByUserFavorites(HttpSession session){

        User user = UserUtils.getUser(session);


        //调用FavoritesService实现查询所有Favorites
        List<Favorites> list = favoritesService.findAll();
        List<Favorites> listTemp = new ArrayList<>();


        //当前用户喜爱的问题Temp
        List<Problemmanagement> problemmanagements = new ArrayList<>();
        //当前用户喜爱的简历
        List<Resumemanagement> resumemanagements = new ArrayList<>();
        //当前用户喜爱的项目
        List<Itemdetails> itemdetails = new ArrayList<>();

        Favorites temp = new Favorites();


        // 找出当前用户的喜爱列表
        for (Favorites favorites : list) {
            if (favorites.getUserId() == user.getId()) {
                listTemp.add(favorites);
            }
        }

        for (Favorites favorites : listTemp) {
            if (favorites.getProbleId() != null || !favorites.getProbleId().equals("")) {
                problemmanagements.add(problemmanagementService.findById(favorites.getProbleId()));
            }
            if (favorites.getItemId() != null || !favorites.getItemId().equals("")) {
                itemdetails.add(itemdetailsService.findById(favorites.getItemId()));
            }
            if (favorites.getResumeId() != null || !favorites.getResumeId().equals("")) {
                resumemanagements.add(resumemanagementService.findById(favorites.getResumeId()));
            }
        }

        temp.setProblemmanagementList(problemmanagements);
        temp.setItemdetailsList(itemdetails);
        temp.setResumemanagementList(resumemanagements);




        return new Result(true, StatusCode.OK,"查询成功",temp) ;
    }
}
